package com.vito.framework.system.menu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vito.framework.base.exception.CommonException;
import com.vito.framework.base.service.impl.BaseServiceImpl;
import com.vito.framework.system.menu.mapper.MenuMapper;
import com.vito.framework.system.menu.model.MenuBean;
import com.vito.framework.system.menu.service.MenuService;
import com.vito.framework.system.role.model.RoleBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuBean> implements MenuService {

	@Resource
	MenuMapper menuMapper;

	@Override
	public MenuBean getMenuById(String menuId)
			throws CommonException {
		return menuMapper.getMenuById(menuId);
	}

	@Override
	public List<MenuBean> getMenuList(Map<String, Object> paramMap)
			throws CommonException {
		return menuMapper.getMenuList(paramMap);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void addMenu(MenuBean menuBean) throws CommonException {
		Integer menuId = Integer.parseInt(getOne(new LambdaQueryWrapper<MenuBean>().orderByDesc(MenuBean::getMenuId)).getMenuId());
		menuBean.setMenuId(String.valueOf(menuId + 1));
		menuBean.setMenuUse("D");
		menuMapper.insertMenu(menuBean);

//		menuMapper.deleteOrgMenu(menuBean.getMenuId());
//
//		if(menuBean.getSiIds() !=null ){
//    		map.put("menuId", menuBean.getMenuId());
//        	for(String si:menuBean.getSiIds()){
//        		map.put("siId", si);
//        		menuMapper.insertOrgMenu(map);
//        	}
//    	}
		Integer count = menuMapper.getMenuChildren(menuBean.getParentMenu());

    	if(count>1){
    		menuBean.setMenuUse("F");
    		menuMapper.updateMenuUse(menuBean);
    	}
    	menuMapper.insertSysRoleMenu(menuBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editMenu(MenuBean menuBean) throws CommonException {
		Map<String, Object> map = new HashMap<>();
		menuMapper.updateMenu(menuBean);

//    	menuMapper.deleteOrgMenu(menuBean.getMenuId());
//
//    	map.put("menuId", menuBean.getMenuId());
//    	if(menuBean.getSiIds() !=null ){
//    		
//        	for(String si:menuBean.getSiIds()){
//        		map.put("siId", si);
//        		menuMapper.insertOrgMenu(map);
//        	}
//        	//删除角色下的菜单
//        	String param_orgIds = "";
//    		for(int z = 0; z<menuBean.getSiIds().length; z++){
//    			if(z == menuBean.getSiIds().length - 1){
//    				param_orgIds = param_orgIds + menuBean.getSiIds()[z];
//    			}else{
//    				param_orgIds = param_orgIds + menuBean.getSiIds()[z] + ",";
//    			}
//    		}
//    		map.put("param_orgIds", param_orgIds);
//    		menuMapper.deleteRoleMenuInfoByOrgs(map);
//    	}else{
//    		menuMapper.deleteRoleMenuInfoById(map);
//    	}
    	
	}
	
	@Override
	public List<RoleBean> getOrgResultList(String menuId) throws CommonException {
		return menuMapper.getOrgResultList(menuId);

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void deleteMenu(Map<String, Object> map) throws CommonException {
			String[] ids = (String[])map.get("ids");
        	if(ids !=null ){
        		menuMapper.deleteOrgMenuChildren(map);
        		menuMapper.deleteRoleMenu(map);
        		menuMapper.deleteMenu(map);
        	}
	}
	
	@Override
	public List<MenuBean> getPmenuList(Map<String, Object> map) throws CommonException {

		return menuMapper.getPmenuList(map);
	}

	@Override
	public void orgMenuSave(String orgMenuId, String[] orgIds) throws CommonException {
		//删除菜单授权机构
		menuMapper.deleteOrgMenu(orgMenuId);
		Map<String, Object> map = new HashMap<>();
		List<String> silds=new ArrayList<String>();
		map.put("menuId", orgMenuId);
		if(orgIds !=null ){
	    	for(String si:orgIds){
	    		JSONObject jsonObject = JSONObject.parseObject(si);
	    		map.put("siId", jsonObject.get("orgId"));
	    		silds.add(jsonObject.get("orgId").toString());
	    		menuMapper.insertOrgMenu(map);
	    	}
		}
		Map<String, Object> paramMap=new HashMap<String, Object>();
		MenuBean menuBean=menuMapper.getMenuById(orgMenuId);
		Integer count = menuMapper.getMenuChildren(menuBean.getParentMenu());

    	if(count>1){
    		menuBean.setMenuUse("F");
    		menuMapper.updateMenuUse(menuBean);
    	}
    	//查询菜单下角色按钮
    	Integer countRoleMenu=menuMapper.countRoleMenuById(orgMenuId);
    	if(countRoleMenu<1) {
    		//新增
        	menuMapper.insertSysRoleMenu(menuBean); 
    	}else {
    		//修改
         	String param_orgIds = "";
         	if(silds != null && silds.size()>0) {
        		for(int z = 0; z<silds.size(); z++){
        			if(z == silds.size()- 1){
        				param_orgIds = param_orgIds +silds.get(z);
        			}else{
        				param_orgIds = param_orgIds +silds.get(z) + ",";
        			}
        		}
        		map.put("param_orgIds", param_orgIds);
        		menuMapper.deleteRoleMenuInfoByOrgs(map);
         	}else {
         		menuMapper.deleteRoleMenuInfoById(map);
         	}
    	}

	}

}
